import { defineStore } from "pinia";
import { confirmOrder, submitOrder, tradeOrder } from "@/api/order";
import { ElMessage } from "element-plus";


const useTradeStore = defineStore("Trade", {
  state: () => {
    return {
      orderInfo: [],
      order:{orderNumber:'',totalPrice:0},
      count: 0,
      total:0
    };
  },
  actions: {
    // 商品详情页点击立即购买
    async confirmOrder(data: any) {
      let result = await confirmOrder(data);
      if (result.code == 200) {
        this.orderInfo = result.data;
        this.count = result.count
        this.total = result.total
        return 'ok'
      } else {
        ElMessage.error(result.msg);
      }
    },
    // 购物车点击结算
    async tradeOrder(data:any){
      let result = await tradeOrder(data)
      if(result.code == 200){
        this.orderInfo = result.data
        this.total = result.total
        this.count = result.count
      }else{
        ElMessage.error(result.msg)
      }
    },
    // 提交订单
    async handleSubmitOrder(data:any){
      let result = await submitOrder(data)
      if(result.code == 200){
        this.order = result.data
        return Promise.resolve('ok');
      }else{
        ElMessage.error(result.msg)
        return Promise.reject(new Error(result.msg));
      }
    }
  },
  getters: {},
});

export default useTradeStore;
